<?php
declare (strict_types=1);

namespace app\api\controller;


use app\model\CategoryModel;
use app\model\StaffModel;
use think\facade\Request;
use think\facade\Validate;

class Category
{
    public function list()
    {
        $categoryList = CategoryModel::select();
        foreach ($categoryList as &$value){
            //操作员
            $staffOne = StaffModel::find($value['staff_id']);
            $value['username'] = $staffOne['username'];
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'categoryList' => $categoryList
            ],
        ];
        return json($data);
    }
    public function add()
    {
        // 1. 获取参数
        // 2. 校验参数
        // 3. 验证文本是否合规。
        // 4. 写入数据库
        // 5. 返回数据


        //order增加菜品分类
        $params = Request::param();
        $validate = Validate::rule([
            'category_name|分类名称' => 'require',
            'category_picture|分类图片路径' => 'require',
            'category_desc|分类描述' => 'require',
            'staff_id|员工id' => 'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

//        if (!$params['staff_id']){
//            $params['staff_id']=null;
//        }
        $params['add_time']=$params['update_time']=time();
        $result=CategoryModel::create($params);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //菜品类型删除
    public function delete()
    {
        $categoryId = Request::param("category_id");
        $validate = Validate::rule([
            'category_id|分类id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['category_id' => $categoryId])) {
            echo $validate->getError();
            exit();
        }
//        $category=CategoryModel::find($categoryId);
        $result = CategoryModel::destroy($categoryId);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //菜品分类修改
    public function update()
    {
        // 1. 获取评论id
        // 2. 修改评论状态status = 2就可以了。
        $params = Request::param();
        $validate = Validate::rule([
            'category_id|分类id' => 'require|between:1,' . PHP_INT_MAX,
            'category_name|分类名称' => 'require',
            'category_picture|分类图片路径' => 'require',
            'category_desc|分类描述' => 'require',
            'staff_id|员工id'=>'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check($params)) {
            echo $validate->getError();
            exit();
        }
        $category = CategoryModel::find($params['category_id']);

        if ($params['category_name']){
            $category['category_name']=$params['category_name'];
        }
        if ($params['category_picture']){
            $category['category_picture']=$params['category_picture'];
        }
        if ($params['category_desc']){
            $category['category_desc']=$params['category_desc'];
        }
        $category['update_time'] = time();
        $result = $category->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? true : false
            ]
        ];
        return json($data);
    }
}
